Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

225
Visualizações
How do I click a javascript "link" using rselenium to start downloading excel file?

I'm a beginner in R and I've been trying to scrape the following website:

https://sinca.mma.gob.cl/cgi-bin/APUB-MMA/apub.htmlindico2.cgi?page=pageFrame&header=Las%20Condes&macropath=./RM/D13/Cal/0001&macro=0001.diario.diario&from=970508&to=090312&

to automatically start downloading an excel file from this air monitoring station here in Chile. However, I noticed that using SelectorGadget the download link couldn't be selected and found out it was because the link was a javascript link (href="javascript:Open('xcl');">Excel CSV) as seen in the image below.

enter image description here

My question is there any way I can simulate a click on that javascript link? I've already simulated clicks for other parts of the same website succesfully but they where not javascript links. This is what I've already tried:

library(rvest)
library(RSelenium)


rD <- rsDriver(browser="firefox", port=4040L, verbose=F)
remDr <- rD[["client"]]


remDr$open()
remDr$navigate("https://sinca.mma.gob.cl/cgi-bin/APUB-MMA/apub.htmlindico2.cgi?page=pageFrame&header=Las%20Condes&macropath=./RM/D13/Cal/0001&macro=0001.diario.diario&from=970508&to=090312&")
Sys.sleep(5) # give the page time to fully load


# click on excel link to start downloading file (doesnt work) ------------

xls <- remDr$findElement("xpath", "/html/body/table/tbody/tr/td/table[2]/tbody/tr[1]/td/label/span[3]/a")
xls$clickElement()
/html/body/table/tbody/tr/td/table[2]/tbody/tr[1]/td/label/span[3]/a


##### TESTING

remDr$navigate("https://sinca.mma.gob.cl/cgi-bin/APUB-MMA/apub.htmlindico2.cgi?page=pageFrame&header=Las%20Condes&macropath=./RM/D13/Cal/0001&macro=0001.diario.diario&from=970508&to=090312&")
excel <- remDr$findElement(using = "css selector", "[href*=javascript]")
#excel <- remDr$findElement(using = "css selector", '[href*="javascript:Open('xcl')"]')
excel$highlightElement() # to visually check what elemnet is selected


remDr$closeServer()
remDr$close()

My objective is to be able to click the link and start downloading the excel file. I've been trying to do this for a couple days and I'm stuck, so any help would be greatly appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It is inside a frame that is inside a frameset. You first need to switch to that frame via the parent frameset, using RSelenium. You can use the frame ids frMain and right to target the elements to switch to. Then you should be able to simply target that href by using a css attribute = value selector of [href*=xcl]. The * is the contains operator specifying that the href attribute value of the target contains the substring xcl.

Something like the following (not tested):

frame_set <- remDr$findElement("css", "#frMain")
remDr$switchToFrame(frame_set)

child_frame <- remDr$findElement("css", "#right")
remDr$switchToFrame(child_frame)

csv_download <- remDr$findElement(using = "css", "[href*=xcl]")
csv_download$clickElement()

You can use browser dev tools F12 to view that path:

enter image description here

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda